from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import time

# 妈的, 淘宝要先登录了
# 用京东演示好了

def rest(sec):
    time.sleep(sec)


if __name__ == '__main__':
    browser = webdriver.Chrome()
    try:
        url = 'https://www.jd.com/'
        browser.get(url)
        # 找到输入框, 输入 iphonex
        input = browser.find_element_by_xpath('//*[@id="key"]')
        input.send_keys('iphonex')
        # 一秒后清空输入
        rest(1)
        # 并输入 苹果笔记本 pro
        input.clear()
        input.send_keys('苹果笔记本 pro')
        # 点击搜索
        rest(1)
        btn_search = browser.find_element_by_xpath('//*[@id="search"]/div/div[2]/button')
        btn_search.click()

        # wait
        wait = WebDriverWait(browser, 10) # 顶多等 10 秒
        # 期待条件的.presense_of_elemeent_located
        wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="J_goodsList"]/ul')))
        print('OK')
    finally:
        rest(5)
        browser.close()
